using System.Collections;
using Castle.MonoRail.Framework;

namespace WineCellar.Web
{
    public abstract class PropertyAccessorBase
    {
        private class Constants
        {
            public const string Messages = "messages";
        }
        private Controller _controller;
        public PropertyAccessorBase()
        {

        }
        public void Initialize(Controller controller)
        {
            this._controller = controller;
        }
        private void InitMessageCollection()
        {
            if (!this.Flash.Contains(Constants.Messages))
            {
                this._controller.Logger.Debug("New Message Bag Created");
                this.Flash[Constants.Messages] = new MessageCollection();
            }
            else
                this._controller.Logger.Debug("Use existing Message Bag");
        }

        private MessageCollection GetMessageCollection()
        {
            this.InitMessageCollection();
            return (MessageCollection)this.Flash[Constants.Messages];
        }
        protected IDictionary PropertyBag
        {
            get { return this._controller.PropertyBag; }

        }
        protected Flash Flash
        {
            get { return this._controller.Flash; }
        }
        public MessageCollection Messages
        {
            get { return this.GetMessageCollection();  }
        }
        public void AddMessage(Message message)
        {
            this.Messages.Add(message);

        }


    }
}
